using System;
using System.Collections.Generic;
using WIT.DAQ.Core.Models;

namespace WIT.DAQ.Core;

/// <summary>
/// WIT.Core传递的参数
/// </summary>
public class DAQArg
{
	/// <summary>
	/// 值的集合
	/// </summary>
	public Dictionary<string, object> Values { get; set; } = new Dictionary<string, object>();


	/// <summary>
	/// 站点
	/// </summary>
	public Station Station { get; }

	/// <summary>
	/// 任务
	/// </summary>
	public DAQTask Task { get; set; }

	/// <summary>
	/// 是否已经自动记录日志
	/// </summary>
	public bool AutoLog { get; }

	private int _logId { get; set; } = -1;


	/// <summary>
	/// 日志Id
	/// </summary>
	public int LogId => _logId;

	/// <summary>
	/// 时间戳
	/// </summary>
	public DateTime Timestamp { get; } = DateTime.Now;


	/// <summary>
	/// 构造函数
	/// </summary>
	public DAQArg()
	{
		_logId = -1;
	}

	/// <summary>
	/// 构造函数
	/// </summary>
	/// <param name="station"></param>
	/// <param name="task"></param>
	/// <param name="autoLog"></param>
	/// <param name="logId"></param>
	public DAQArg(Station station, DAQTask task, bool autoLog, int logId)
	{
		Station = station;
		Task = task;
		AutoLog = autoLog;
		_logId = logId;
	}

	/// <summary>
	/// 构造函数
	/// </summary> 
	/// <param name="autoLog"></param>
	/// <param name="logId"></param>
	public DAQArg(bool autoLog, int logId)
	{
		AutoLog = autoLog;
		_logId = logId;
	}

	/// <summary>
	/// 添加日志记录（仅在未自动记录日志的情况下生效，会自动更新LogId属性）
	/// </summary>
	public void AddLogProcess()
	{
		if (!AutoLog && Station != null && Task != null)
		{
			_logId = Common.AddLogProcess(Station.Id, Station.Name, Task.Id, Task.Name);
		}
	}

	/// <summary>
	/// 添加日志记录（仅在未自动记录日志的情况下生效，会自动更新LogId属性）
	/// </summary>
	/// <param name="stationName"></param>
	/// <param name="taskName"></param>
	public void AddLogProcess(string stationName, string taskName)
	{
		if (!AutoLog)
		{
			_logId = Common.AddLogProcess(Station.Name, Task.Name);
		}
	}

	/// <summary>
	/// 添加日志明细（不存在主表数据时会自动创建）
	/// </summary>
	/// <param name="content"></param>
	public void AddLogProcessDetail(string content)
	{
		if (LogId <= 0)
		{
			AddLogProcess();
		}
		Common.AddLogProcessDetail(LogId, content);
	}

	/// <summary>
	/// 日志完结（仅在未自动记录日志的情况下生效）
	/// </summary>
	public void FinishLogProcess()
	{
		if (!AutoLog && Station != null && Task != null)
		{
			Common.FinishLogProcess(LogId);
		}
	}

	/// <summary>
	/// 日志完结（仅在未自动记录日志的情况下生效）
	/// </summary>
	/// <param name="duration">任务耗时(s)</param>
	public void FinishLogProcess(double duration)
	{
		if (!AutoLog && Station != null && Task != null)
		{
			Common.FinishLogProcess(LogId, duration);
		}
	}
}
